package com.haitaoit.chengquan.module.center.adapter.order;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyOrderDetailModel;
import com.haitaoit.chengquan.view.MyListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */
public class MyReturnAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<MyOrderDetailModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyReturnAdapter(Context context) {
        this.context = context;
    }

    public List<MyOrderDetailModel> getList() {
        return list;
    }

    public void setList(List<MyOrderDetailModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_return_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.item_ll:
                    case R.id.item_ll2:
                        MyReturnAdapter.this.backCall.deal(R.id.item_ll, position);
                        break;
                    case R.id.cancel_apply:
                        MyReturnAdapter.this.backCall.deal(R.id.cancel_apply, position);
                        break;
                    case R.id.again_apply:
                        MyReturnAdapter.this.backCall.deal(R.id.again_apply, position);
                        break;
                    case R.id.detele_order:
                        MyReturnAdapter.this.backCall.deal(R.id.detele_order, position);
                        break;
                }
            }
        };
        holder.item_ll.setOnClickListener(listener);
        holder.item_ll2.setOnClickListener(listener);
        holder.cancel_apply.setOnClickListener(listener);// 取消申请
        holder.again_apply.setOnClickListener(listener);// 再次申请
        holder.detele_order.setOnClickListener(listener);// 删除订单
        holder.cancel_apply.setVisibility(View.GONE);
        holder.again_apply.setVisibility(View.GONE);
        holder.detele_order.setVisibility(View.GONE);

        holder.return_no.setText("订单号：" + list.get(position).getOrder_no());
        // status:退款状态(1退款中显示取消 2退款成功显示删除订单 3退款失败显示再次申请)
        if (list.get(position).getStatus().equals("1")) {
            holder.return_status.setText("退款中");
            holder.cancel_apply.setVisibility(View.VISIBLE);
        } else if (list.get(position).getStatus().equals("2")) {
            holder.return_status.setText("退款成功");
            holder.detele_order.setVisibility(View.VISIBLE);
        } else if (list.get(position).getStatus().equals("3")) {
            holder.return_status.setText("退款失败");
            holder.again_apply.setVisibility(View.VISIBLE);
        }

        MyReturnAdapter2 adapter2 = new MyReturnAdapter2(context);
        adapter2.setList(list.get(position).getList_goods());
        holder.scrollview_listview.setAdapter(adapter2);
        holder.scrollview_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                MyReturnAdapter.this.backCall.deal(R.id.scrollview_listview, position);
            }
        });

        holder.order_amount.setText("交易金额：¥ " + list.get(position).getOrder_amount());

        // 部分字体变大变色
        SpannableString msp = new SpannableString("¥ " + list.get(position).getRefund_money());
        msp.setSpan(new RelativeSizeSpan(1.5f), 1, ("¥ " + list.get(position).getRefund_money()).length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.return_money.setText(msp);
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.item_ll)
        LinearLayout item_ll;
        @ViewInject(R.id.item_ll2)
        LinearLayout item_ll2;
        @ViewInject(R.id.return_money)
        TextView return_money;
        @ViewInject(R.id.cancel_apply)
        TextView cancel_apply;
        @ViewInject(R.id.again_apply)
        TextView again_apply;
        @ViewInject(R.id.detele_order)
        TextView detele_order;
        @ViewInject(R.id.return_no)
        TextView return_no;
        @ViewInject(R.id.return_status)
        TextView return_status;
        @ViewInject(R.id.scrollview_listview)
        MyListView scrollview_listview;
        @ViewInject(R.id.order_amount)
        TextView order_amount;
    }
}
